package com.atguigu.jxc.controller;


import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.SaleList;
import com.atguigu.jxc.service.SaleListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 销售管理
 */
@RestController
@RequestMapping("/saleListGoods")
public class SaleListGoodsController {

    @Autowired
    private SaleListGoodsService saleListGoodsService;

    //http://localhost:8080/saleListGoods/save?saleNumber=XS1605772263999 （销售单号）
    //销售单保存
    @PostMapping("/save")
    public ServiceVO save(SaleList saleList, String saleListGoodsStr, HttpSession session){
        try {
            saleListGoodsService.save(saleList,saleListGoodsStr,session);
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE,ErrorCode.PARA_TYPE_ERROR_MESS);
        }
    }

    //http://localhost:8080/saleListGoods/list
    //销售单查询（可查询条件：销售单号、客户、付款状态）
    @PostMapping("/list")
    public Map<String,Object> list(String saleNumber, Integer customerId, Integer state, String sTime,String eTime){
          return saleListGoodsService.list(saleNumber,customerId,state,sTime,eTime);
    }

    //http://localhost:8080/saleListGoods/goodsList
    //销售单商品信息 Integer saleListId
    @PostMapping("/goodsList")
    public Map<String,Object> goodsList(Integer saleListId){
        return saleListGoodsService.goodsList(saleListId);
    }

    //http://localhost:8080/saleListGoods/delete
    //删除销售单 Integer saleListId
    @PostMapping("/delete")
    public ServiceVO delete(Integer saleListId){
        try {
            saleListGoodsService.delete(saleListId);
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
    }

    //http://localhost:8080/saleListGoods/updateState
    //支付结算（修改销售单付款状态） Integer saleListId
    @PostMapping("/updateState")
    public ServiceVO updateState(Integer saleListId){
        try {
            saleListGoodsService.updateState(saleListId);
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
    }

    //http://localhost:8080/saleListGoods/count
    //销售统计（可根据 商品类别、商品编码或名称 条件查询）
    @PostMapping("/count")
    public String count(String sTime, String eTime ,Integer goodsTypeId, String codeOrName){
        return   saleListGoodsService.count(sTime,eTime,goodsTypeId,codeOrName);
    }

    //http://localhost:8080/saleListGoods/getSaleDataByDay
    //按日统计接口
    @PostMapping("/getSaleDataByDay")
    public String getSaleDataByDay(String sTime, String eTime){
       return saleListGoodsService.getSaleDataByDay(sTime,eTime);
    }

    //http://localhost:8080/saleListGoods/getSaleDataByMonth
    //按月统计接口
    @PostMapping("/getSaleDataByMonth")
    public String getSaleDataByMonth(String sTime, String eTime){
        return saleListGoodsService.getSaleDataByMonth(sTime,eTime);
    }
}
